package com.glsc.ngateway.ldap.service.smartbi;


import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Base64;

/**
 * token工具类
 */
public class SecurityUtils {
    // 获得密钥
    public static SecretKey getKey(String s) throws Exception {
        SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
        DESKeySpec ks = new DESKeySpec(s.getBytes());
        SecretKey kd = kf.generateSecret(ks);
        return kd;
    }

    // --------------------------------------------------------------------------------------------------
    // 返回加密后的字符串
    // key是用于生成密钥的字符串，input是要加密的字符串
    public static String getEncryptedString(String input, String key) {
        String base64 = "";
        try {
            Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, getKey(key));
            byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8);
            byte[] outputBytes = cipher.doFinal(inputBytes);
            Base64.Encoder encoder = Base64.getEncoder();
            base64 = encoder.encodeToString(outputBytes);
        } catch (Exception e) {
            base64 = e.getMessage();
        }
        return base64;
    }

    // --------------------------------------------------------------------------------------------------
    // 返回解密后的字符串
    // key是用于生成密钥的字符串，input是要解密的字符串
    public static String getDecryptedString(String input, String key) throws Exception {
        String result = null;
        Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, getKey(key));
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] raw = decoder.decode(input);
        byte[] stringBytes = cipher.doFinal(raw);
        result = new String(stringBytes, StandardCharsets.UTF_8);
        return result;
    }

    //加密
    public static String getEncryptedString2(String input, String key) {
        String as = SecurityUtils.getEncryptedString(input, key);
        try {
            return URLEncoder.encode(as, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //解密
    public static String getDecryptedString2(String input, String key) {
        try {
            System.out.println(URLDecoder.decode(input, "UTF-8"));
            return SecurityUtils.getDecryptedString(URLDecoder.decode(input, "UTF-8"), key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        try {
            String str = getDecryptedString2("VPKWvUZJsNRGKKXDL67jK7njOq%2BiYvymdwN2ZHsLzCnJIjCUXIA3aa4BUPkE%2BXsY", "1234567890");
            System.out.println("解密：" + str);
            JSONObject json = new JSONObject();
            json.put("username", "admin");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            json.put("datetime", System.currentTimeMillis());
            String as = SecurityUtils.getEncryptedString2(json.toString(), "1234567890");
            System.out.println("加密后：" + as);


        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
